/**
 * @ Author: luoqi
 * @ Create Time: 2024-07-26 23:40
 * @ Modified by: luoqi
 * @ Modified time: 2024-07-27 00:20
 * @ Description:
 */

#include <stdint.h>
#include <stdlib.h>
#include <bsp.h>
#include "blink.h"
#include "../qshell/qterm.h"

TX_THREAD task_blink;
static uint8_t task_blink_stack[256];
uint32_t blink_frq;

static void task_blink_entry(uint32_t arg)
{
    static uint32_t cnt = 0;
    blink_frq = arg;
    while(1) {
        if(cnt++ > blink_frq) {
            cnt = 0;
            bsp_blink();
        }
        tx_thread_sleep(1);
    }
}

static QCmdObj _cmd_blink;
static int _cmd_blink_hdl(int argc, char **argv)
{
    if(argc == 1) {
        QSH(" -frq <ms>\r\n");
    } else if(ISARG(argv[1], "frq") && (argc == 3)) {
        int frq = atoi(argv[2]);
        blink_frq = frq;
    } else {
        return -1;
    }
    return 0;
}

int blink_init()
{
    qterm_attach(&_cmd_blink, "blink", _cmd_blink_hdl, "frq <ms>");
    return tx_thread_create(&task_blink, "blink",
        task_blink_entry, 1000, task_blink_stack, sizeof(task_blink_stack),
        3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
}

void blink_frq_set(uint32_t frq)
{
    blink_frq = frq;
}
